package com.qk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.dto.BusinessDto;
import com.qk.dto.BusinessPoolDto;
import com.qk.entity.Business;
import com.qk.entity.BusinessTrackRecord;
import com.qk.entity.Customer;
import com.qk.mapper.BusinessMapper;
import com.qk.mapper.BusinessTrackRecordMapper;
import com.qk.mapper.CustomerMapper;
import com.qk.service.BusinessService;
import com.qk.utils.CurrentUserHoler;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {

    @Autowired
    private BusinessTrackRecordMapper businessTrackRecordMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public PageResult<Business> selectPage(BusinessDto businessDto) {
        Page<Business> page = baseMapper.listPage(new Page<>(businessDto.getPage(), businessDto.getPageSize()),businessDto);
        return new PageResult<>(page.getTotal(), page.getRecords());
    }

    @Override
    public Business getBusinessById(Integer id) {
        return baseMapper.getBusinessById(id);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateBusiness(Business business) {
        //1.跟新商机
        business.setStatus(3);
        business.setUpdateTime(LocalDateTime.now());
        baseMapper.updateById(business);
        //2.新增商机记录
        BusinessTrackRecord businessTrackRecord = new BusinessTrackRecord();
        businessTrackRecord.setBusinessId(business.getId());
        businessTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());
        businessTrackRecord.setTrackStatus(business.getTrackStatus());
        businessTrackRecord.setKeyItems(business.getKeyItems().toString());
        businessTrackRecord.setNextTime(business.getNextTime());
        businessTrackRecord.setRecord(business.getRecord());
        businessTrackRecord.setCreateTime(LocalDateTime.now());

        businessTrackRecordMapper.insert(businessTrackRecord);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void toCustomer(Integer id) {
        //根据id查询信息
        Business business = baseMapper.getBusinessById(id);

        business.setStatus(5);
        business.setUpdateTime(LocalDateTime.now());
        baseMapper.updateById(business);

        Customer customer = new Customer();
        BeanUtils.copyProperties(business, customer);
        customer.setId(null);
        customer.setBusinessId(id);
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());

        customerMapper.insert(customer);
    }

    @Override
    public PageResult<Business> listPage(BusinessPoolDto businessPoolDto) {
        LambdaQueryWrapper<Business> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(businessPoolDto.getId() != null,Business::getId, businessPoolDto.getId())
                .like(businessPoolDto.getName() != null,Business::getName, businessPoolDto.getName())
                .like(businessPoolDto.getPhone() != null,Business::getPhone, businessPoolDto.getPhone()).
                eq(businessPoolDto.getSubject() != null,Business::getSubject, businessPoolDto.getSubject())
                        .eq(Business::getStatus,4);
        Page<Business> page = baseMapper.selectPage(new Page<>(businessPoolDto.getPage(), businessPoolDto.getPageSize()), queryWrapper);
        return new PageResult<>(page.getTotal(),page.getRecords());
    }
}
